package com.ybkj.daijia.api.web.v1;

import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.common.IdcardUtils;
import com.ybkj.daijia.common.Result;
import com.ybkj.daijia.server.model.DriverApply;
import com.ybkj.daijia.server.service.AppVersionService;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.CouponService;
import com.ybkj.daijia.server.service.DriverApplyService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.MemberAccountService;
import com.ybkj.daijia.server.service.MemberConsumedRecordService;
import com.ybkj.daijia.server.service.MemberPaymentsRecordService;
import com.ybkj.daijia.server.service.MemberRechargeRecordService;
import com.ybkj.daijia.server.service.MemberService;
import com.ybkj.daijia.server.service.OrderService;
import java.io.FileNotFoundException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

@Controller
@RequestMapping(value = "web/v1/")
public class WebController {

    private static final Logger logger = LoggerFactory.getLogger(WebController.class);

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private DriverService driverService;

    @Autowired
    private MemberAccountService memberAccountService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private MemberRechargeRecordService memberRechargeRecordService;

    @Autowired
    private MemberConsumedRecordService memberConsumedRecordService;

    @Autowired
    private MemberPaymentsRecordService memberPaymentsRecordService;

    @Autowired
    private CouponService couponService;

    @Autowired
    private AppVersionService appVersionService;

    @Autowired
    private DriverApplyService driverApplyService;

    @Autowired
    private ApplicationEventMulticaster applicationEventMulticaster;


    /**
     * 申请服务人员
     *
     * @return
     */
    @RequestMapping("driverApply")
    public ModelAndView areaDetail() {

        ModelAndView mv = new ModelAndView();

        mv.setViewName("front/driverapply");

        return mv;
    }

    /**
     * 申请服务人员
     *
     * @return
     */
    @RequestMapping(value = "driverApply", method = RequestMethod.POST)
    public synchronized @ResponseBody
    Result<DriverApply> driverApply(
        String realname, String idCard, String phone, String introducer, String memo,
        String photo, String license,
        HttpServletRequest req) {

        logger.debug("recv realname:{},idCard:{},phone:{},introducer{},memo{}",
            realname, idCard, phone, introducer);

        Result<DriverApply> vo = new Result<DriverApply>();
        vo.setSuccess(false);

        if (StringUtils.isBlank(realname)) {
            vo.setMessage("真实姓名不能为空");
            return vo;
        }

        if (StringUtils.isBlank(idCard)) {
            vo.setMessage("身份证不能为空");
            return vo;
        }

        if (!IdcardUtils.validateCard(idCard)) {
            vo.setMessage("身份证格式不正确");
            return vo;
        }

        if (null != driverService.findByIdCard(idCard)) {
            vo.setMessage("身份证已存在");
            return vo;
        }

        if (null != driverApplyService.findByIdCard(idCard)) {
            vo.setMessage("服务人员申请已提交，等待审核");
            return vo;
        }

        if (StringUtils.isBlank(phone)) {
            vo.setMessage("联系电话不能为空");
            return vo;
        }

        if (null != driverService.findByPhone(phone)) {
            vo.setMessage("联系电话已存在");
            return vo;
        }

        if (null != driverApplyService.findByPhone(phone)) {
            vo.setMessage("服务人员申请已提交，等待审核");
            return vo;
        }

        try {

            DriverApply driverApply = new DriverApply();
//            driverApply.setGender(IdcardUtils.getGenderByIdCard(idCard));
            driverApply.setIdCard(idCard);
            driverApply.setPhone(phone);

            if (StringUtils.isNotBlank(realname)) {
                realname = realname.replaceAll("<", "&lt").replace(">", "&gt");
            }
            driverApply.setRealname(realname);

            if (StringUtils.isNotBlank(introducer)) {
                introducer = introducer.replaceAll("<", "&lt").replace(">", "&gt");
            }
            driverApply.setIntroducer(introducer);

            if (StringUtils.isNotBlank(memo)) {
                memo = memo.replaceAll("<", "&lt").replace(">", "&gt");
            }
            driverApply.setMemo(memo);

            if (StringUtils.isNotBlank(photo)) {
                driverApply.setPhoto(photo);
            }
            if (StringUtils.isNotBlank(license)) {
                driverApply.setLicensePhoto(license);
            }

            driverApplyService.insertRecord(driverApply);

            vo.setSuccess(true);

            return vo;

        } catch (Exception e) {
            e.printStackTrace();
            vo.setMessage("内部服务器出错");
            return vo;
        }

    }

    private String getBasePath(HttpServletRequest req) {

        try {
            return WebUtils.getRealPath(req.getSession().getServletContext(), "/");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return StringUtils.EMPTY;
    }
}
